package com.zhongmai.yzfgrabanorder.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.zhongmai.yzfgrabanorder.R;
import com.zhongmai.yzfgrabanorder.base.BaseActivity;
import com.zhongmai.yzfgrabanorder.bean.GrabOrderBean;
import com.zhongmai.yzfgrabanorder.utils.Constants;
import com.zhongmai.yzfgrabanorder.utils.EncryptUtils;
import com.zhongmai.yzfgrabanorder.utils.HttpXUtils;
import com.zhongmai.yzfgrabanorder.utils.Utils;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.http.RequestParams;

import butterknife.BindView;
import butterknife.OnClick;

import static com.zhongmai.yzfgrabanorder.utils.Constants.user;

/**
 * 申诉信息页面
 */

public class OrderInfoActivity extends BaseActivity implements View.OnClickListener {

    @BindView(R.id.mTV_title)
    TextView mTV_title;
    @BindView(R.id.iv_back)
    ImageView iv_back;
    @BindView(R.id.mTV_order_id)
    TextView mTV_order_id;
    @BindView(R.id.mTV_order_time)
    TextView mTV_order_time;
    @BindView(R.id.mTV_payee)
    TextView mTV_payee;
    @BindView(R.id.mTV_order_bank)
    TextView mTV_order_bank;
    @BindView(R.id.mTV_bank_no)
    TextView mTV_bank_no;
    @BindView(R.id.mTV_order_money)
    TextView mTV_order_money;
    @BindView(R.id.mTV_bank_address)
    TextView mTV_bank_address;
    @BindView(R.id.mLL_handle)
    LinearLayout mLL_handle;
    @BindView(R.id.mTV_deal_time)
    TextView mTV_deal_time;
    @BindView(R.id.mTV_order_result)
    TextView mTV_order_result;
    @BindView(R.id.mTV_order_remark)
    TextView mTV_order_remark;
    @BindView(R.id.mTV_Handler)
    TextView mTV_Handler;
    @BindView(R.id.mTV_payee_copy)
    TextView mTV_payee_copy;
    @BindView(R.id.mTV_bank_no_copy)
    TextView mTV_bank_no_copy;
    @BindView(R.id.mTV_order_money_copy)
    TextView mTV_order_money_copy;
    @BindView(R.id.mTV_bank_address_copy)
    TextView mTV_bank_address_copy;

    @BindView(R.id.mRL_bottom)
    RelativeLayout mRL_bottom;
    @BindView(R.id.mBT_confirmed_order)
    Button mBT_confirmed_order;
    @BindView(R.id.mBT_back_order)
    Button mBT_back_order;
    @BindView(R.id.mBT_success)
    Button mBT_success;
    GrabOrderBean bean;

    @Override
    protected void initLayout() {
        setContentView(R.layout.activity_order_info);
    }

    @Override
    protected void initView() {
        setStatusBar();
        mTV_title.setText("订单详情");
        iv_back.setVisibility(View.VISIBLE);

    }

    @OnClick({R.id.iv_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                this.finish();
                break;
        }
    }

    @Override
    protected void initData() {
        isQuit = false;
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        if (bundle != null) {
            String order = bundle.getString("order");
            Gson gson = new Gson();
            bean = gson.fromJson(order, GrabOrderBean.class);
            mTV_order_id.setText(bean.getOrderId());
            mTV_order_time.setText(Utils.formatTime(bean.getStartTime()));
            mTV_payee.setText(bean.getBusiness());
            mTV_payee_copy.setOnClickListener(this);
            mTV_order_bank.setText(bean.getBusinessBank());
            mTV_bank_no.setText(bean.getBusinessCard());
            mTV_bank_no_copy.setOnClickListener(this);
            mTV_order_money.setText(bean.getMoney());
            mTV_order_money_copy.setOnClickListener(this);
            String openingBank = bean.getOpeningBank();
            if (!TextUtils.isEmpty(openingBank)) {
                mTV_bank_address.setText(openingBank);
                mTV_bank_address_copy.setOnClickListener(this);
            } else {
                mTV_bank_address_copy.setVisibility(View.INVISIBLE);
            }
            if (bean.getState().equals("4")) {
                mBT_confirmed_order.setOnClickListener(this);
                mBT_back_order.setOnClickListener(this);
                mBT_success.setOnClickListener(this);
                mLL_handle.setVisibility(View.INVISIBLE);
            } else {
                mTV_payee_copy.setVisibility(View.INVISIBLE);
                mTV_bank_no_copy.setVisibility(View.INVISIBLE);
                mTV_order_money_copy.setVisibility(View.INVISIBLE);
                mTV_bank_address_copy.setVisibility(View.INVISIBLE);
                mRL_bottom.setVisibility(View.INVISIBLE);
                mTV_deal_time.setText(Utils.formatTime(bean.getEndTime()));
                switch (bean.getState()) {
                    case "0":
                        mTV_order_result.setText("成功");
                        mTV_order_result.setTextColor(getResources().getColor(R.color.order_mode_success));
                        mTV_order_remark.setText(bean.getProcessResult());
                        break;
                    case "6":
                        mTV_order_result.setText("待确认");
                        mTV_order_result.setTextColor(getResources().getColor(R.color.order_mode_confirmed));
                        mTV_order_remark.setText(bean.getProcessResult());
                        break;
                    default:
                        break;
                }
                mTV_Handler.setText(user);
            }
        }
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.mTV_payee_copy:
                Utils.setClipboard(this, mTV_payee.getText().toString().trim());
                showToast("《收款姓名》复制成功");
                break;
            case R.id.mTV_bank_no_copy:
                Utils.setClipboard(this, mTV_bank_no.getText().toString().trim());
                showToast("《银行卡号》复制成功");
                break;
            case R.id.mTV_order_money_copy:
                Utils.setClipboard(this, mTV_order_money.getText().toString().trim());
                showToast("《金额》复制成功");
                break;
            case R.id.mTV_bank_address_copy:
                Utils.setClipboard(this, mTV_bank_address.getText().toString().trim());
                showToast("《开户行》复制成功");
                break;
            case R.id.mBT_confirmed_order:
                showNormalDialog("6", "修改订单状态为“待确认”，请求管理员进行人工处理");
                break;
            case R.id.mBT_back_order:
                showNormalDialog("4", "退回订单，暂不处理该笔订单");
                break;
            case R.id.mBT_success:
                showNormalDialog("0", "修改订单状态为成功并扣除商户余额");
                break;
            default:
                break;
        }
    }

    private void showNormalDialog(final String s, final String msg) {
        if (s.equals("6")) {
            LayoutInflater inflater = (LayoutInflater) OrderInfoActivity.this.getSystemService(LAYOUT_INFLATER_SERVICE);
            final View view = inflater.inflate(R.layout.dialog_edittext_layout, null);
            new AlertDialog.Builder(OrderInfoActivity.this)
                    .setTitle("温馨提示")
                    .setMessage(msg)
                    .setView(view)
                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            EditText et = view.findViewById(R.id.mET);
                            String userMsg = et.getText().toString().trim();
                            logMag(userMsg);
                            reportResult(s, userMsg);
                        }
                    }).setNegativeButton("取消", null).show();
        } else {
            AlertDialog.Builder dialog = new AlertDialog.Builder(this);
            dialog.setTitle("温馨提示").setMessage(msg);
            //点击确定就退出程序
            dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    reportResult(s, "");
                }
            });
            //如果取消，就什么都不做，关闭对话框
            dialog.setNegativeButton("取消", null);
            dialog.show();
        }
    }

    private void reportResult(String s, String remarks) {

        RequestParams params = new RequestParams(Constants.Url + Constants.updateState);
        try {
            params.addHeader("Authorization", user);
            JSONObject json = new JSONObject();

            json.put("orderId", bean.getOrderId());
            json.put("state", s);
            json.put("processResult", remarks);
            String def = EncryptUtils.encrypt(json.toString(), Constants.encrypt_key, Constants.encrypt_iv);
            String mdf = EncryptUtils.md5(def + Constants.encrypt_key).toUpperCase();
            params.addBodyParameter("def", def);
            params.addParameter("mdf", mdf);
        } catch (JSONException e1) {
            e1.printStackTrace();
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        HttpXUtils.xUtilsPostRequest(OrderInfoActivity.this, params, new HttpXUtils.MyCallback() {
            @Override
            public void myResult(String result) {
                JSONObject jsonObject = null;
                try {
                    jsonObject = new JSONObject(result);
                    String code = jsonObject.getString("code");
                    if (code.equals("100000")) {
                        String jsonData = jsonObject.getString("data");
                        JSONObject j = new JSONObject(jsonData);
                        String msg = j.getString("msg");
                        if (msg.equals("订单处理成功")) {
                            logMag("处理成功");
                            Intent intent = getIntent();
                            setResult(RESULT_OK, intent);
                            finish();
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(String err) {

            }
        }, true);
    }
}
